// 判断登陆状态
class judgment {
    constructor() {
        this.temporary();
    }
    temporary() {

        if (getCookie('pass') != '') {
            let getName = JSON.parse(getCookie('pass'));
            $('header .content-right a').html('欢迎').css('color', 'red');
            $('header .content-right p a').html(getName.id).css('color', 'red');
            if ($('section .cart-body').length == 0) {
                $('section .null').show();
                $('section .cart-header').hide();
                $('section .cart-footer').hide();
            }
        } else {
            $('section .null').show();
            $('section .cart-header').hide();
            $('section .cart-footer').hide();
            $('section .cart-body').hide();
            alert('请先登录')
        }
    }
}
new judgment();


// 推荐部分
class recommend {
    constructor() {
        this.move();
    }
    move() {
        $('main ul li').on('mouseenter', function () {
            $('main .content ul div').eq($(this).index('main .content ul li')).stop().animate({
                bottom: 23
            })
        })
        $('main .content ul li').on('mouseleave', function () {
            $('main .content ul li>div').stop().animate({
                bottom: -33
            })
        })
    }
}
new recommend();

// 购物车
class shoppingCart {
    constructor() {
        this.getAllGoods();
        this.newGoodsAdd();
        this.getAGoodsSum();
        this.changeNum();
        this.goodsDel();
        this.allChoose();
        this.goodsNum();
        this.oneChoose();
        this.getGoodsPriceAndUpdate();
        this.pay();
        this.setAllGoods();
    }
    // 打开页面时 先连接数据库拉取数据
    getAllGoods() {
        // 拉取
        let that=this;
        let id= JSON.parse(getCookie('pass')).id;
        let n=localStorage.key(0);
        let m=localStorage.getItem(id);
            if(m!=null&&id==n){
                m=JSON.parse(m);
                console.log(m);
                for(let i=0;i<m.length;i++){
                  let name=m[i].name;
                  let img=m[i].img;
                  let price=m[i].price;
                  $('section .cart-header').after(`<div class="cart-body">
                  <div class="choose">
                      <input type="checkbox">
                  </div>
                  <div class="goodsImg">
                     ${img}
                  </div>
                  <div class="goodsName">
                     ${name}
                  </div>
                  <div class="goodsPrice">
                      <span>${price}</span>元
                  </div>
                  <div class="goodsNmu">
                      <div>
                         <button class="btnMin">-</button>
                         <span>1</span>
                         <button class="btnAdd">+</button>
                      </div>
                  </div>
                  <div class="goodsSum">
                      <span>${price}</span>元
                  </div>
                  <div class="goodsDel">
                      x
                  </div>  
              </div>`);
              $('section .cart-footer .number span').html($('section .cart-body').length);
              if ($('section .cart-body').length != 0) {
                  $('section .null').hide();
                  $('section .cart-header').show();
                  $('section .cart-footer').show();
              }
             
              that.changeNum();
              that.goodsDel();
              that.allChoose();
              that.goodsNum();
              that.oneChoose();
              that.getAGoodsSum();
              that.getGoodsPriceAndUpdate();
              that.pay();



                }
            }
    }
    //  新物品添加  获取主页cookie  并上传数据库
    newGoodsAdd() {
       
        if (getCookie('goods') != ''){
            let that = this;
            let newGoods = JSON.parse(getCookie('goods'));

            let newName = newGoods.name;
            let oldName = '';

            let timer = setInterval(function () {
                {
                    newGoods = JSON.parse(getCookie('goods'));
                    newName = newGoods.name;
                    if (oldName != newName) {
                        $('section .cart-header').after(`<div class="cart-body">
                         <div class="choose">
                             <input type="checkbox">
                         </div>
                         <div class="goodsImg">
                             <img src="${newGoods.img}" alt="">
                         </div>
                         <div class="goodsName">
                            ${newGoods.name}
                         </div>
                         <div class="goodsPrice">
                             <span>${newGoods.price}</span>元
                         </div>
                         <div class="goodsNmu">
                             <div>
                                <button class="btnMin">-</button>
                                <span>1</span>
                                <button class="btnAdd">+</button>
                             </div>
                         </div>
                         <div class="goodsSum">
                             <span>${newGoods.price}</span>元
                         </div>
                         <div class="goodsDel">
                             x
                         </div>  
                     </div>`);
                        oldName = newName;
                        $('section .cart-footer .number span').html($('section .cart-body').length);
                        if ($('section .cart-body').length != 0) {
                            $('section .null').hide();
                            $('section .cart-header').show();
                            $('section .cart-footer').show();
                        }
                       
                        that.changeNum();
                        that.goodsDel();
                        that.allChoose();
                        that.goodsNum();
                        that.oneChoose();
                        that.getAGoodsSum();
                        that.getGoodsPriceAndUpdate();
                        that.pay();
                    }
                }
            }, 1000);
            // document.addEventListener('visibilitychange', function () { });
        }else{}
    }

    //每个商品的数量小计
    //总共购物车有多少个商品
    //每个商品的价格小计
    getAGoodsSum() {
        let that = this;
        $('section .cart-body .btnAdd').on('click', function () {
            let num = $(this).prev().html();
            let price = $(this).parent().parent().prev().children().html();
            let result = ++num * price;
            $(this).parent().parent().next().children().html(result);
        })
        $('section .cart-body .btnMin').on('click', function () {
            let num = $(this).next().html();
            let price = $(this).parent().parent().prev().children().html();
            let result = --num * price;
            if (result < 0) {
                result = 0
            }
            $(this).parent().parent().next().children().html(result);
        })
        $('section .cart-body button').on('click', function () {
            that.getGoodsPriceAndUpdate();
        });
    }


    //所有商品花了多少钱
    getGoodsPriceAndUpdate() {
        //  计算选定物品价格总计
        let getPrice = 0;
        let arr = [];
        getPrice = $('section .cart-body .choose input:checked').parent().next().next().next().next().next().children().html();
        let n = Number(getPrice);
        arr.push(n);
        $('section .cart-body .choose input').map(function (i, k) {
            if ($(k).prop('checked') == true) {
                getPrice = $(this).parent().next().next().next().next().next().children().html();
                let n = Number(getPrice);
                arr.push(n);
            }
        })
        let x = 0;
        for (let i = 0; i < arr.length; i++) {
            x = x + arr[i]
            if(x<=0)[
                x=0
            ]
        }
        // console.log(x);
        $('section .cart-footer .sum span').html(x);
        arr = [];


        $('section .cart-body .choose input,section .cart-header input').on('change', function () {
            $('section .cart-body .choose input').map(function (i, k) {
                if ($(k).prop('checked') == true) {
                    getPrice = $(this).parent().next().next().next().next().next().children().html();
                    let n = Number(getPrice);
                    arr.push(n);
                }
            })
            let x = 0;
            for (let i = 0; i < arr.length; i++) {
                x = x + arr[i];
                if(x<=0)[
                    x=0
                ]
            }
            // console.log(x);
            $('section .cart-footer .sum span').html(x);
            arr = [];
            
        })



    }
    // 全选
    allChoose() {
        // 全选 $('section .cart-header input');
        // 单选 $('section .cart-body .choose input');
        // 操作选中 .attr("checked", 'true');
        // 操作不选 .attr("checked", 'false');
        // 判断选中.is(':checked')


        $('section .cart-header input').on('change', function () {
            if ($(this).prop('checked') == true) {
                $('section .cart-body .choose input').prop('checked', true);
                $('section .cart-header input').prop('checked', true);
            } else {
                $('section .cart-body .choose input').prop('checked', false);
                $('section .cart-header input').prop('checked', false);
            }
            $('section .cart-footer .number b').html($('section .cart-body .choose input:checked').length);

        });

    }
    // 单选
    oneChoose() {
        // 选中商品
        $('section .cart-body .choose input').on('change', function () {
            // 选中
            let checked_num = 0;
            $('section .cart-body .choose input').map((i, k) => {
                if ($(k).prop('checked') == true) {
                    checked_num++
                }
            })
            if (checked_num == $('section .cart-body .choose input').length) {
                $('section .cart-header input').prop('checked', true)
            } else {
                $('section .cart-header input').prop('checked', false)
            }
            $('section .cart-footer .number b').html($('section .cart-body .choose input:checked').length);
        })
    }
    goodsNum() {
        // 共多少商品
        $('section .cart-footer .number span').html($('section .cart-body').length);
        //  删除时调用
        $('section .cart-body .goodsDel').on('click', function () {
            $('section .cart-footer .number span').html($('section .cart-body').length);
            $('section .cart-footer .number b').html($('section .cart-body .choose input:checked').length);

        });

    }

    //btn + -
    changeNum() {
        $('section .cart-body .btnAdd').off("click").on('click', function () {
            let num = $(this).prev().html();
            num++;
            $(this).prev().html(num)
        });

        $('section .cart-body .btnMin').off("click").on('click', function () {
            let num = $(this).next().html();
            num--;
            if (num < 0) { num = 0 };
            $(this).next().html(num);
        });
    }
    //删除商品
    goodsDel() {
        let that = this;
        $('section .cart-body .goodsDel').on('click', function () {
            $(this).parent().remove();
            that.getGoodsPriceAndUpdate();
            if ($('section .cart-body').length == 0) {
                $('section .null').show();
                $('section .cart-header').hide();
                $('section .cart-footer').hide();
            }
        })
    }
    // 结算
    pay() {
        $('section .cart-footer .end').on('click', function () {
            $('body,html').stop().animate({
                scrollTop:0
            })
            $('#pay').show();
            $('body').css({ "overflow-y": "hidden" });
            $('#pay span').html($('section .cart-footer .sum span').html())
        })
        $('#pay b').on('click', function () {
            $('#pay').hide();
            $('body').css({ "overflow-y": "scroll" })
        })
    }
    // 储存物品
    setAllGoods(){
        let oldL=0;
        let arr=[];
        $('body,html').on('mousemove',function(){
            let newL=$('section .cart-body').length;
            
         
            if(newL!=oldL){
            // 存
            //    名字
            let obj={};
               $('section .cart-body .goodsName').html();
            //    图片地址
               $('section .cart-body .goodsImg').html();
            // 价格
               $('section .cart-body .goodsPrice span').html()
                obj={'name':$('section .cart-body .goodsName').html(),'img': $('section .cart-body .goodsImg').html(),'price':$('section .cart-body .goodsPrice span').html()}
               if(obj!=undefined){
                arr.push(obj);
                let id= JSON.parse(getCookie('pass')).id;
                let str=JSON.stringify(arr);
                localStorage.setItem(id,str);
               }
                oldL=newL;
            }
        })
    }
}
new shoppingCart();
// 顶部




